package com.yaopaikeji.mapper;

import com.yaopaikeji.model.EmailGroupInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EmailGroupMapper {

    //根据主键获取group信息
    @Select("select * from email_group_info where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "group_name",property = "groupName"),
            @Result(column = "group_desc",property = "groupDesc"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "id",property = "members",
            many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findUsersByGroupId"))
    })
    EmailGroupInfo findGroupByPrimaryKey(int id);

    //获取所有group
    @Select("select * from email_group_info")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "group_name",property = "groupName"),
            @Result(column = "group_desc",property = "groupDesc"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "id",property = "members",
                    many = @Many(select = "com.yaopaikeji.mapper.UserGroupRelationMapper.findUsersByGroupId"))
    })
    List<EmailGroupInfo> findGroupsAll();

    @Insert("INSERT INTO email_group_info (" +
            "                                group_name," +
            "                                group_desc," +
            "                                create_time" +
            ")" +
            "VALUES" +
            "(" +
            "                                #{groupName}," +
            "                                #{groupDesc}," +
            "                                #{createTime}" +
            ");")
    @SelectKey(statement = "select last_insert_id()",keyProperty = "id",before = false,resultType = int.class)
    int insertEmailGroup(EmailGroupInfo emailGroupInfo);

    @Update("<script>" +
            "    update email_group_info " +
            "    <set>" +
            "        <if test='group_name != null'>" +
            "            group_name = #{groupName}," +
            "        </if>" +
            "        <if test='group_desc'>" +
            "            group_desc = #{groupDesc}" +
            "        </if>" +
            "    </set>" +
            "    where id = #{id}" +
            "</script>")
    EmailGroupInfo updateEmailGroup(EmailGroupInfo emailGroupInfo);

    //删除邮件组
    @Delete("delete from email_group_info where id = #{id}")
    int deleteEmailGroup(int id);


}
